{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "conservative-context",
   "metadata": {},
   "source": [
    "# Indian Digital Resident Personal Identity Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "third-chambers",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sustained-humidity",
   "metadata": {},
   "source": [
    "The function `clean_in_aadhaar()` cleans a column containing Indian digital resident personal identity number (Aadhaar) strings, and standardizes them in a given format. The function `validate_in_aadhaar()` validates either a single Aadhaar strings, a column of Aadhaar strings or a DataFrame of Aadhaar strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "technical-costs",
   "metadata": {},
   "source": [
    "Aadhaar strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"234123412346\"\n",
    "* `standard`: Aadhaar strings with proper whitespace in the proper places, like \"2341 2341 2346\"\n",
    "* `mask`: mask the first 8 digits as per MeitY guidelines for securing identity information and Sensitive personal data, like \"XXXX XXXX 2346\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_in_aadhaar()` and `validate_in_aadhaar()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "decimal-genius",
   "metadata": {},
   "source": [
    "### An example dataset containing Aadhaar strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "recovered-exemption",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"aadhaar\": [\n",
    "            \"234123412346\",\n",
    "            \"643343121\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "spiritual-glory",
   "metadata": {},
   "source": [
    "## 1. Default `clean_in_aadhaar`\n",
    "\n",
    "By default, `clean_in_aadhaar` will clean aadhaar strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bacterial-cylinder",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_in_aadhaar\n",
    "clean_in_aadhaar(df, column = \"aadhaar\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "backed-holder",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dutch-surfing",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mechanical-malawi",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "human-karma",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_in_aadhaar(df, column = \"aadhaar\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "severe-messaging",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adopted-works",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_in_aadhaar(df, column = \"aadhaar\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "finite-river",
   "metadata": {},
   "source": [
    "### `mask`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "widespread-samba",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_in_aadhaar(df, column = \"aadhaar\", output_format=\"mask\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stunning-stick",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned Aadhaar strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "seven-stanley",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_in_aadhaar(df, column=\"aadhaar\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "specialized-basis",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "social-russell",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "floating-stanford",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_in_aadhaar(df, \"aadhaar\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "pediatric-potter",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "future-farming",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_in_aadhaar(df, \"aadhaar\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "communist-winning",
   "metadata": {},
   "source": [
    "## 4. `validate_in_aadhaar()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "frequent-associate",
   "metadata": {},
   "source": [
    "`validate_in_aadhaar()` returns `True` when the input is a valid Aadhaar. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_in_aadhaar()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_in_aadhaar()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_in_aadhaar()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "curious-robert",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_in_aadhaar\n",
    "print(validate_in_aadhaar(\"234123412346\"))\n",
    "print(validate_in_aadhaar(\"643343121\"))\n",
    "print(validate_in_aadhaar(\"999 999 999\"))\n",
    "print(validate_in_aadhaar(\"51824753556\"))\n",
    "print(validate_in_aadhaar(\"004085616\"))\n",
    "print(validate_in_aadhaar(\"hello\"))\n",
    "print(validate_in_aadhaar(np.nan))\n",
    "print(validate_in_aadhaar(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sunset-islam",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "worthy-hayes",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_in_aadhaar(df[\"aadhaar\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forty-finnish",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "racial-progressive",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_in_aadhaar(df, column=\"aadhaar\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "remarkable-bikini",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "military-democrat",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_in_aadhaar(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "difficult-defendant",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
